<script setup>
import * as api from "@/api/v-sys-service/exam"
import {useRoute} from "vue-router";

const route = useRoute()

const examId = route.query.examId

const paperId = route.query.paperId

const examInfo = ref({
  examName: '',
  examScore: '',
  passScore: '',
  examResult: '',
  questionTotal: '',
  carefulNotice: '',
})

const queType = ref({
  1: "单选题",
  2: "多选题",
  3: "判断题",
})

const paperContent = ref([])

const loading = ref({
  examResultLoading: false,
})

const fillAnswer = computed(()=> {
  return (item) => {
    const answers = item.answer
    const answer = answers && Array.isArray(answers) ? answers.join(",") : answers
    return [1, 2].includes(item.type) ? item.queContent.replace(/\（.*?\）/g,`(${answer})`) : item.queContent
  }
})

/**
 * 获取考题
 */
const getExamById = async () => {
  const { code, data } = await api.getExamById(examId)
  if (code === 200) {
    examInfo.value.examName = data.examName
    examInfo.value.fullScore = data.fullScore
    examInfo.value.passScore = data.passScore
    examInfo.value.questionTotal = data.questionTotal
    examInfo.value.carefulNotice = data.carefulNotice
    const answerContent = JSON.parse(data.answerContent)
    for (const key in answerContent) {
      answerContent[key].rightTags = answerContent[key].rightTags.join("、")
    }
    paperContent.value = JSON.parse(data.paperContent).map(ele => {
      ele.rightAnswer = answerContent[ele.queId].rightTags
      ele.answer = ele.type ===  2 ? [] : ''
      return ele
    })

    await getPaperById()
  }
}

/**
 * 查询考试成绩
 */
const getPaperById = async () => {
  loading.value.examResultLoading = true
  const {code, data} = await api.getPaperById(paperId)
  if (code === 200) {
    examInfo.value.examResult = data.examResult === 2 ? '不及格' : '及格'
    examInfo.value.examScore = data.examScore
    await getSubmitAnswersByPaperId()
  }
  loading.value.examResultLoading = false
}

/**
 * 获取试卷答题内容
 */
const getSubmitAnswersByPaperId = async () => {
  const {code, data} = await api.getSubmitAnswersByPaperId(paperId)
  if (code === 200) {
    paperContent.value.forEach(ele => {
      const item = data[ele.queId]
      ele.answer = ele.type === 2 ? item.submitContent.split(",") : item.submitContent
      ele.answerStatus = item.answerStatus
      ele.answerScore = item.answerScore
    })
    let sum = 0
    paperContent.value.map(ele => {
      ele.answerStatus === 2 ? sum+=ele.answerScore : sum +=0
    })
  }
}

const init = () => {
  getExamById()
}

/**
 * 组件挂载后
 */
onMounted(() => {
  init()
})
</script>

<template>
  <div>
    <div class="exam-result" v-loading="loading.examResultLoading">
      <div class="paper-title">
        <div class="exam-name flex justify-center">
          <div class="title flex-start">
            <span>{{ examInfo.examName }}</span>
          </div>
        </div>
        <div class="flex align-center">
          <div class="title flex">
            <div style="width: 5em;white-space: nowrap;height: 100%">注意事项: &emsp;</div>
            <div>
              {{ examInfo.carefulNotice }}
            </div>
          </div>
        </div>
        <div class="flex align-center">
          <div class="title">
            共: <span>{{ examInfo.questionTotal }} 小题&ensp;</span>
          </div>
          <div class="title">
            满分: <span>{{ examInfo.fullScore }} 分&ensp;</span>
          </div>
          <div class="title">
            及格分: <span>{{ examInfo.passScore }}&ensp;</span>
          </div>
          <div class="title">
            得分: <span>{{ examInfo.examScore }}</span>
          </div>
        </div>
      </div>
      <div class="paper-content">
        <div class="que-card" v-for="(item,index) in paperContent">
          <div class="que">
            <span>{{index + 1}}．</span>
            <span>{{ fillAnswer(item) }}</span>
            <span>&nbsp&nbsp( {{item.score}} 分)</span>
            <span style="font-size: 18px; color: red;">{{ item.answerStatus === 2 ? '√' : '×' }}</span>
          </div>
          <div class="answer">
            <el-radio-group v-model="item.answer" v-if="[1, 3].includes(item.type)">
              <el-radio disabled v-for="option in item.options" :label="`${option.tag}：${option.content}`" :value="option.tag"/>
            </el-radio-group>
            <el-checkbox-group v-model="item.answer" v-if="item.type === 2">
              <el-checkbox disabled v-for="option in item.options" :label="`${option.tag}：${option.content}`" :value="option.tag" />
            </el-checkbox-group>
          </div>
          <div class="right-box" v-if="item.answerStatus === 3">
            <div class="right-answer">
              <span>正确答案：</span>
              <span>{{ item.rightAnswer }}</span>
            </div>
            <div class="answer-analysis">
              <span>解析：</span>
              <div>{{item.answerAnalysis}}</div>
            </div>
          </div>
        </div>
        <div class="que-btn flex justify-between">
        </div>
      </div>
    </div>
  </div>
</template>
<style scoped lang="scss">
.exam-result {
  padding: 5px 10px;
  width: 400px;
  .paper-title {
    padding: 5px 10px 0;
    .exam-name {
      margin: 10px 0;
      .title {
        font-size: 17px;
        font-weight: bold;
      }
    }
    .title {
      font-weight: bold;
      font-size: 12px;
    }
  }
  .paper-content {
    padding: 10px;
    height: 300px;
    .que-card {
      .que {
        color: #606266;
        font-size: 12px;
        .type {
          font-weight: bold;
        }
      }
      .answer {
        :deep(.el-radio) {
          .el-radio__label {
            font-size: 12px;
          }
          .el-radio__input.is-disabled.is-checked .el-radio__inner {
            background-color: #00a7ff !important;
            border-color: #00a7ff !important;
          }
        }
        :deep(.el-checkbox) {
          .el-checkbox__label {
            font-size: 12px;
          }
          .el-checkbox__input.is-disabled.is-checked .el-checkbox__inner {
            background-color: #00a7ff !important;
            border-color: #00a7ff !important;
          }
        }
      }
      .right-box {

        .right-answer {
          span {
            font-size: 12px;
            color: red;
          }
        }
        .answer-analysis {
          span {
            font-size: 14px;
            font-weight: bold;
            color: red;
          }
          div {
            padding-right: 20px;
            font-size: 12px;
            color: red;
            margin-bottom: 5px;
          }
        }
      }

    }
    .que-btn {

    }
  }
}
</style>
